package com.common.utils;



import com.common.base.BasePO;
import com.common.base.BaseVO;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @类名 BeanCopyUtil
 * @描述 TODO
 * @版本 1.0
 * @创建人 shichen
 * @创建时间 2020/2/9
 * @修改人 shichen
 * @修改时间 2020/2/9
 **/
public class BeanCopyUtils {

    public static <TP extends BasePO,TV extends BaseVO> TV POToVO(TP po, Class<TV> voClazz) throws Exception {
        if(null==po){
            throw new Exception("转换失败，po为null");
        }
        TV vo = null;
        try {
            vo=voClazz.newInstance();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }catch (Exception e){
            throw new Exception("PO转VO失败");
        }
    }


    public static <TP extends BasePO,TV extends BaseVO> TP VOToPO(TV vo, Class<TP> poClazz) throws Exception {
        if(null==vo){
            throw new Exception("转换失败，po为null");
        }
        TP po = null;
        try {
            po=poClazz.newInstance();
            BeanUtils.copyProperties(vo,po);
            return po;
        }catch (Exception e){
            throw new Exception("VO转PO失败");
        }
    }

    public static <TP extends BasePO,TV extends BaseVO> TP VOToPOIfInsert(TV vo, Class<TP> poClazz) throws Exception {
        if(null==vo){
            throw new Exception("转换失败，po为null");
        }
        TP po = null;
        try {
            po=poClazz.newInstance();
            BeanUtils.copyProperties(vo,po);
            po.setCreateTime(new Date());
            po.setUpdateTime(new Date());
            po.setVersion(1);
            po.setDeleted(false);
            return po;
        }catch (Exception e){
            throw new Exception("VO转PO失败");
        }
    }

    public static <TP extends BasePO,TV extends BaseVO> TP VOToPOIfUpdate(TV vo, Class<TP> poClazz) throws Exception {
        if(null==vo){
            throw new Exception("转换失败，po为null");
        }
        TP po = null;
        try {
            po=poClazz.newInstance();
            BeanUtils.copyProperties(vo,po);
            po.setUpdateTime(new Date());
            if (vo.getVersion()!=null){
                po.setVersion(vo.getVersion()+1);
            }
            return po;
        }catch (Exception e){
            throw new Exception("VO转PO失败");
        }
    }

    public static <TP extends BasePO,TV extends BaseVO> List<TV> POListToVOList(List<TP> poList ,Class<TV> voClazz) throws Exception {
        if(null==poList){
            throw new Exception("转换失败，poList为null");
        }
        ArrayList<TV> voList = new ArrayList<>();
        try{
            Iterator<TP> iterator = poList.iterator();
            while (iterator.hasNext()){
                TP po = iterator.next();
                TV vo = voClazz.newInstance();
                BeanUtils.copyProperties(po,vo);
                voList.add(vo);
            }
            return voList;
        }catch (Exception e){
            throw new Exception("poList转voList失败");
        }
    }
    public static <TP extends BasePO,TV extends BaseVO> List<TP> VOListToPOList(List<TV> voList ,Class<TP> poClazz) throws Exception {
        if(null==voList){
            throw new Exception("转换失败，voList为null");
        }
        ArrayList<TP> poList = new ArrayList<>();
        try{
            Iterator<TV> iterator = voList.iterator();
            while (iterator.hasNext()){
                TV vo = iterator.next();
                TP po = poClazz.newInstance();
                BeanUtils.copyProperties(vo,po);
                poList.add(po);
            }
            return poList;
        }catch (Exception e){
            throw new Exception("voList转poList失败");
        }
    }
}